package cn.edu.zafu.bigdata.weier;

import java.net.*;

public class TimeClient {
    public static void main(String[] args) {
        try {
            InetAddress serverAddress = InetAddress.getByName("192.168.16.69"); // 替换为服务端的IP地址
            DatagramSocket clientSocket = new DatagramSocket();
            String clientMessage = "Time Request";
            byte[] sendData = clientMessage.getBytes();
            byte[] receiveData = new byte[1024];

            // 发送请求到服务端
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, 1234);
            clientSocket.send(sendPacket);

            // 接收服务端的响应
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);

            // 显示服务端返回的时间
            String serverResponse = new String(receivePacket.getData(), 0, receivePacket.getLength());
            System.out.println("Server response: " + serverResponse);

            clientSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}